from base.vertice import Vertice
from base.graph import Graph
from dijkstra import Dijkstra

class Pair:
    def __init__(self, x, y):
            self.x = x
            self.y = y
    def __add__(self, other):
            return Pair(self.x + other.x, self.y + other.y)
    def __lt__(self, other):
            return self.x < other.x if self.x != other.x else self.y < other.y 
    def __repr__(self):
            return '('+str(self.x)+','+str(self.y)+')'

w = dict()  # dicionario dos pesos das arestas, associa um peso a cada aresta
# peso: (t - tempo, d - distancia)

def weight(pair):
    if pair in w:
        return w[pair]

vertices = [
            Vertice('A'),
            Vertice('B'),
            Vertice('C'),
            Vertice('D'),
            Vertice('E'),
            ]

g = Graph(vertices)

# adicionando arestas entre os vertices do grafo

# adjacencias A

g.add_edge(vertices[0], vertices[1])
w[(vertices[0], vertices[1])] = Pair(3, 3)
g.add_edge(vertices[0], vertices[3])
w[(vertices[0], vertices[3])] = Pair(5, 5)

# adjacencias B

g.add_edge(vertices[1], vertices[2])
w[(vertices[1], vertices[2])] = Pair(6, 2)
g.add_edge(vertices[1], vertices[3])
w[(vertices[1], vertices[3])] = Pair(2, 2)

# adjacencias C

g.add_edge(vertices[2], vertices[4])
w[(vertices[2], vertices[4])] = Pair(2, 2)

# adjacencias D

g.add_edge(vertices[3], vertices[2])
w[(vertices[3], vertices[2])] = Pair(4, 5)
g.add_edge(vertices[3], vertices[1])
w[(vertices[3], vertices[1])] = Pair(1, 3)
g.add_edge(vertices[3], vertices[4])
w[(vertices[3], vertices[4])] = Pair(6, 9)

# adjacencias E
g.add_edge(vertices[4], vertices[2])
w[(vertices[4], vertices[2])] = Pair(7, 4)
g.add_edge(vertices[4], vertices[0])
w[(vertices[4], vertices[0])] = Pair(3, 6)

print('Menor caminho usando dijkstra entre a origem e o destino:')

path = ''
result_set = Dijkstra(Pair(0,0), Pair(float('inf'),float('inf'))).dijkstra(g, weight, vertices[3])    
for v in result_set[vertices[0]][::-1]:
    path += str(v.name) + ', '

print(path)
print('Custo: ', vertices[0].key)